package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class CarShow extends Sprite
	{
		private var cars:Array = new Array();
		
		private var r:Render;
		private var layer1:ImageSprite;
		private var layer2:ImageSprite;
		private var layer3:ImageSprite;
		private var close:ImageSprite;
		private var right:ImageSprite;
		private var left:ImageSprite;
		
		public var showing = false;
		private var loaded:Boolean = false;
		private var position:int = 0;
		public function CarShow(r:Sprite)
		{
			this.r = r as Render;
			init();
		}
		public function newIM(w:int,x:int,y:int,fl:String):ImageSprite{
			var i:ImageSprite = new ImageSprite();
			if (w!=0)
			i.overrideSize(w,0);
			i.x=x;
			i.y=y;
			addChild(i);
			i.load(null,fl);
			return i;
		}
		public function init(){
			layer1 = newIM(0,0,0,"assets/images/CarShow/base.jpg");
			layer2 = newIM(0,0,0,"assets/images/CarShow/mid.png");
			layer3 = newIM(0,0,0,"assets/images/CarShow/top.png");
			
			right = newIM(40,1100,200,"assets/images/gui/HUD_ArrowR.png");
			left = newIM(40,50,200,"assets/images/gui/HUD_ArrowL.png");
			right.addEventListener(MouseEvent.CLICK,carRight);
			left.addEventListener(MouseEvent.CLICK,carLeft);
			close = newIM(20,Render.SCR_WID-20,0,"assets/images/gui/HUD_Cancel.png");
			close.addEventListener(MouseEvent.CLICK,hide);
		}
		private function carRight(e:Event):void{
			if (contains(cars[position] as Car))
			removeChild(cars[position] as Car);
			drawCar(position+1);
		}
		private function carLeft(e:Event):void{
			if (contains(cars[position] as Car))
			removeChild(cars[position] as Car);
			drawCar(position-1);
		}
		public function show():void{
			//loadCars();
			r.addChild(this);
			showing=true;
			carLeft(null);
			trace("SHOW");
			
		}
		public function hide(e:Event = null):void{
			r.removeChild(this);
			showing=false;
			loaded=false;
			position=0;
		}
		public function loadCars():void{
			var myTextLoader:URLLoader = new URLLoader();
			
			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			function onLoaded(e:Event):void {
				var myArrayOfLines:Array = e.target.data.split(/\n/);
				setupCars(myArrayOfLines);
			}
			myTextLoader.load(new URLRequest("assets/images/CarShow/cars.txt"));
		}
		private function setupCars(ar:Array):void{
			for each (var st:String in ar){
				var pass:String = st.substr(0,st.lastIndexOf(","));
				cars.push(new Car(0,0).setAll(pass));
				(cars[cars.length-1] as Car).y=100;
				(cars[cars.length-1] as Car).x=Render.SCR_WID/2-200;
				(cars[cars.length-1] as Car).mouseChildren=false;
				(cars[cars.length-1] as Car).mouseEnabled=false;
			}
			loaded=true;
		}
		private function drawCar(i:int):void{
			i%=cars.length;
			if (i<0)i+=cars.length;
			trace(i);
			(cars[i] as Car).shrink(500);
			addChild(cars[i] as Car);
			position=i;
		}
	}
}